package com.joolun.web.controller.promotion;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joolun.system.domain.EduCourseHour;
import com.joolun.system.domain.vo.EduCourseChapterVo;
import com.joolun.system.service.IEduCourseHourService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.joolun.common.annotation.Log;
import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.common.enums.BusinessType;
import com.joolun.system.domain.EduCourseChapter;
import com.joolun.system.service.IEduCourseChapterService;
import com.joolun.common.utils.poi.ExcelUtil;
import com.joolun.common.core.page.TableDataInfo;

/**
 * 课程章节Controller
 * 
 * @author joolun
 * @date 2025-02-27
 */
@RestController
@RequestMapping("/promotion/chapter")
public class EduCourseChapterController extends BaseController
{
    @Autowired
    private IEduCourseChapterService eduCourseChapterService;
    @Autowired
    private IEduCourseHourService eduCourseHourService;
    /**
     * 查询课程章节列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EduCourseChapter eduCourseChapter)
    {
        startPage();
        List<EduCourseChapterVo> listChapter = eduCourseChapterService.selectEduCourseChapterList(eduCourseChapter);
        QueryWrapper<EduCourseHour> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*");
        queryWrapper.inSql("chapter_id","select c.chapter_id from edu_course_chapter c");
        List<EduCourseHour> hours = eduCourseHourService.list(queryWrapper);
        if(hours.size() > 0){
            listChapter.forEach(item->{
                List<EduCourseHour> hoursFilter = hours.stream().filter(hour->hour.getChapterId()==item.getChapterId()).collect(Collectors.toList());
                if(hoursFilter.size() > 0){
                    item.setList(hoursFilter);
                }
            });
        }
        return getDataTable(listChapter);
    }

    /**
     * 导出课程章节列表
     */
    @PreAuthorize("@ss.hasPermi('promotion:chapter:export')")
    @Log(title = "课程章节", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EduCourseChapter eduCourseChapter)
    {
        List<EduCourseChapterVo> list = eduCourseChapterService.selectEduCourseChapterList(eduCourseChapter);
        ExcelUtil<EduCourseChapterVo> util = new ExcelUtil<>(EduCourseChapterVo.class);
        util.exportExcel(response, list, "课程章节数据");
    }

    /**
     * 获取课程章节详细信息
     */
    @GetMapping(value = "/{chapterId}")
    public AjaxResult getInfo(@PathVariable("chapterId") Long chapterId)
    {
        return success(eduCourseChapterService.selectEduCourseChapterByChapterId(chapterId));
    }

    /**
     * 新增课程章节
     */
    @PreAuthorize("@ss.hasPermi('promotion:chapter:add')")
    @Log(title = "课程章节", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EduCourseChapter eduCourseChapter)
    {
        eduCourseChapter.setCreateBy(getUsername());
        return toAjax(eduCourseChapterService.insertEduCourseChapter(eduCourseChapter));
    }

    /**
     * 修改课程章节
     */
    @PreAuthorize("@ss.hasPermi('promotion:chapter:edit')")
    @Log(title = "课程章节", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EduCourseChapter eduCourseChapter)
    {
        eduCourseChapter.setCreateBy(getUsername());
        return toAjax(eduCourseChapterService.updateEduCourseChapter(eduCourseChapter));
    }

    /**
     * 删除课程章节
     */
    @PreAuthorize("@ss.hasPermi('promotion:chapter:remove')")
    @Log(title = "课程章节", businessType = BusinessType.DELETE)
	@DeleteMapping("/{chapterIds}")
    public AjaxResult remove(@PathVariable Long[] chapterIds)
    {
        return toAjax(eduCourseChapterService.deleteEduCourseChapterByChapterIds(chapterIds));
    }
}
